package com.bsy.common.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.widget.FrameLayout
import androidx.viewpager2.widget.ViewPager2
import kotlin.math.abs
import kotlin.math.sign

/**
 * Created by Sincerity on 2024/6/5.
 * Copyright
 * 描述：
 */
class NestedScrollableHost @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private var touchSlop: Int = ViewConfiguration.get(context).scaledTouchSlop
    private var initialX: Float = 0f
    private var initialY: Float = 0f

    private fun getParentViewPager(): ViewPager2? {
        var v = parent as? View
        while (v != null && v !is ViewPager2) {
            v = v.parent as? View
        }
        return v as? ViewPager2
    }

    private fun getChild(): View? {
        return if (childCount > 0) getChildAt(0) else null
    }

    private fun canChildScroll(orientation: Int, delta: Float): Boolean {
        val direction = -delta.sign.toInt()
        return when (orientation) {
            0 -> getChild()?.canScrollHorizontally(direction) ?: false
            1 -> getChild()?.canScrollVertically(direction) ?: false
            else -> false
        }
    }

    override fun onInterceptTouchEvent(e: MotionEvent?): Boolean {
        handleInterceptTouchEvent(e)
        return super.onInterceptTouchEvent(e)
    }

    private fun handleInterceptTouchEvent(e: MotionEvent?) {
        val parentViewPager = getParentViewPager()
        if (parentViewPager != null) {
            val orientation = parentViewPager.orientation
            if (canChildScroll(orientation, -1.0f) || canChildScroll(orientation, 1.0f)) {
                when (e?.action) {
                    MotionEvent.ACTION_DOWN -> {
                        initialX = e.x
                        initialY = e.y
                        parent.requestDisallowInterceptTouchEvent(true)
                    }

                    MotionEvent.ACTION_MOVE -> {
                        val dx = e.x - initialX
                        val dy = e.y - initialY
                        val isVpHorizontal = orientation == ViewPager2.ORIENTATION_HORIZONTAL
                        val scaledDx = abs(dx) * (if (isVpHorizontal) 0.5f else 1.0f)
                        val scaledDy = abs(dy) * (if (isVpHorizontal) 1.0f else 0.5f)
                        if (scaledDx > touchSlop.toFloat() || scaledDy > touchSlop.toFloat()) {
                            if (isVpHorizontal == (scaledDy > scaledDx)) {
                                parent.requestDisallowInterceptTouchEvent(false)
                            } else if (canChildScroll(orientation, if (isVpHorizontal) dx else dy)) {
                                parent.requestDisallowInterceptTouchEvent(true)
                            } else {
                                parent.requestDisallowInterceptTouchEvent(false)
                            }
                        }
                    }
                }
            }
        }
    }

    init {
        touchSlop = ViewConfiguration.get(context).scaledTouchSlop
    }
}


